package ishaportalserver.apps.insight;


import ishaportalserver.apps.Utils;
import ishaportalserver.shared.apps.insight.CenterProp;
import ishaportalserver.shared.rest.DeleteResponse;
import ishaportalserver.shared.rest.PutResponse;
import ishaportalserver.shared.rest.RestStatus;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

@Path("/center/")
public class CenterAPI {
	
	@GET
	@Path("/list/")
	public String list(@QueryParam("callback") String callback) {
		
		Repository repository = new Repository();
		List<CenterProp> allProps = repository.getAllCenters();

		String json = Utils.toJson(allProps, callback);
		return json;
	}
	
	@PUT
	@Path("/add/")
	public String add(@QueryParam("name") String name, 
			@QueryParam("callback") String callback) {
		
		if ((null == name) || name.equals("")) {
			PutResponse resp = new PutResponse();
			resp.statusCode = RestStatus.Code.RESOURCE_NOT_FULLY_SPECIFIED;
			resp.message = "Query parameter [name] is not specified";
			return Utils.toJson(resp, callback);
		}
		
		Repository repository = new Repository();
		PutResponse resp = repository.addCenter(name);
		
		String json = Utils.toJson(resp, callback);
		return json;						
	}
	
	@GET
	@Path("{id}") 
	public String get(@PathParam("id") long id, 
			@QueryParam("callback") String callback) {
		
		Repository repository = new Repository();
		CenterProp prop = repository.getCenter(id);
		
		String json = Utils.toJson(prop, callback);
		return json;
	}
	
	@PUT
	@Path("/update/")
	public String update(@QueryParam("id") long id, 
			@QueryParam("name") String name, @QueryParam("callback") String callback) {
		
		//check if newName is specified
		if ((null == name) || name.equals("")) {
			PutResponse resp = new PutResponse();
			resp.statusCode = RestStatus.Code.RESOURCE_NOT_FULLY_SPECIFIED;
			resp.message = "Query parameter [newname] is not specified";			
			return Utils.toJson(resp, callback);
		}
		
		//check if id is specified
		if (0 == id) {
			PutResponse resp = new PutResponse();
			resp.statusCode = RestStatus.Code.RESOURCE_NOT_FULLY_SPECIFIED;
			resp.message = "Query parameter [id] is not specified";			
			return Utils.toJson(resp, callback);
		}
		
		Repository repository = new Repository();
		PutResponse resp = repository.updateCenter(id, name);
		
		return Utils.toJson(resp, callback);						
	}	
	
	@DELETE
	@Path("/delete/")
	public String delete(@QueryParam("id") long id, 
			@QueryParam("callback") String callback) {
		
		DeleteResponse resp = new DeleteResponse();
		if (0 == id) {
			resp.statusCode = RestStatus.Code.RESOURCE_NOT_FULLY_SPECIFIED;
			resp.message = "Query param [id] is either not specified or 0";
			return Utils.toJson(resp, callback);			
		}
		
		Repository repository = new Repository();
		resp = repository.deleteCenter(id);
		
		return Utils.toJson(resp, callback);		
	}
}
